package com.captainbank.tcl.tclapplication.utils;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    //手机号码4-7位星号处理
    public static String setStarPhone(String phone) {
        if (phone != null && phone.length() >= 8) {
            return phone.substring(0, 3) + "****" + phone.substring(7);
        } else {
            return phone;
        }
    }

    //字符串除首位外星号处理
    public static String setStarName(String name) {
        if (name != null && name.length() > 0) {
            StringBuilder sb = new StringBuilder();
            for(int i=0; i<name.length()-1; i++){
                sb.append("*");
            }
            return name.substring(0,1) + sb;
        } else {
            return name;
        }
    }

    public static boolean checkIsNotNull(String... str) {
        if (str == null || str.length == 0) {
            return false;
        }
        for (int i = 0; i < str.length; i++) {
            if (!checkIsNotNull(str[i])) {
                return false;
            }
        }
        return true;
    }

    public static boolean checkIsNotNull(String str) {
        if (str == null || str.trim().equals("")) {
            return false;
        }
        return true;
    }

    public static boolean regMaches(String regEx, String matchStr) {
        if (!checkIsNotNull(matchStr))
            return false;
        Pattern pat = Pattern.compile(regEx);
        Matcher mat = pat.matcher(matchStr);
        return mat.find();
    }

    public static String strFirstCharToUpCase(String oldStr) {
        return oldStr.substring(0, 1).toUpperCase() + oldStr.substring(1);
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 根据经纬度计算两点间的距离
     * LatBegin 起始纬度
     * LngBegin 起始经度
     * LatEnd 终点纬度
     * LngEnd 终点经度
     */
    public static double Distance(double LatBegin, double LngBegin, double LatEnd, double LngEnd) {
        double distance = 0;
        double earth_radius = 6378.137;
        double RadLatBegin, RadLatEnd, RadLngBegin, RadLngEnd, dx;
        RadLatBegin = LatBegin * Math.PI / 180.0;
        RadLatEnd = LatEnd * Math.PI / 180.0;
        RadLngBegin = LngBegin * Math.PI / 180.0;
        RadLngEnd = LngEnd * Math.PI / 180.0;
        dx = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((RadLatBegin - RadLatEnd) / 2), 2)
                + Math.cos(RadLatBegin) * Math.cos(RadLatEnd) * Math.pow(Math.sin((RadLngBegin - RadLngEnd) / 2), 2)));
        distance = earth_radius * dx;
        return distance;
    }
}
